// import qs from 'qs';
import axios from 'axios'
import global from './global';
import store from '@/store'
// import global from '@/js/global'

axios.defaults.timeout = 3000;   //响应时间
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';        //配置请求头
axios.defaults.baseURL = global.server;   //配置接口地址
//POST传参序列化(添加请求拦截器)
axios.interceptors.request.use((config) => {
    //在发送请求之前做某件事
    const token = store.state.token
    if (token != '') {
        config.headers = {
            'access-token': token,
            'Content-Type': 'application/json'
        };
    }
    if(config.data!=undefined && config.data.params){
        config.data = config.data.params
    }
    config.data = JSON.stringify(config.data);
    return config;
}, (error) => {
    return Promise.reject(error);
});
//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) => {
    //对响应数据做些事
    if (!res.data.success) {
        let newToken = res.data.token;    //成功后更新token
        localStorage.setItem('access_token', newToken);
    }
    return res;
}, (error) => {
    if (error.response.data.status == '401') {    //如果token 过期 则跳转到登录页面
        this.$router.push('/login');
    }
    return Promise.reject(error);
});

//返回一个Promise(发送post请求)
export function post(url, params) {
    return new Promise((resolve, reject) => {
        // url = 'http://localhost:8094' + url;
        // console.log('请求处理完地址url', url);
        axios.post(url, params)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error);
            });
    });
}

//返回一个Promise(发送get请求)
export function get(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, { params: param })
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error);
            });
    });
}
//返回一个Promise(发送put请求)
export function put(url, param) {
    return new Promise((resolve, reject) => {
        axios.put(url, { params: param })
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error);
            });
    });
}
//返回一个Promise(发送delete请求)
export function del(url, param) {
    return new Promise((resolve, reject) => {
        axios.delete(url, { params: param })
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error);
            });
    });
}

//返回一个Promise(发送delete请求)
export function waiwang(url, param) {
    return new Promise((resolve, reject) => {
        axios.post("http://whtx5z.natappfree.cc/"+url, { params: param })
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error);
            });
    });
}